var util=require('util.js')
var willupload=[];
var succupload=[];
var inupload=false;
var errnum=0;
var upload=function(file,callback){
  if (errnum>3){
    util.showinfo("上传出错");
  }
  if (file){
    willupload.push(file);
  }
  if (willupload.length==0){
    callback(succupload);
  }
  if (inupload){
    util.showinfo("已添加上传列表");
  }
  var inupload = true;
  wx.uploadFile({
    url: wx.getStorageSync('host') + '/file/uploadImage', //仅为示例，非真实的接口地址
    filePath: willupload[0],
    name: 'image',
    formData: {
      'shop': 'test'
    },
    success(res) {
      const data = JSON.parse(res.data);
      succupload.push(data.data);
      willupload.splice(0,1);
      inupload = false; 
      if (willupload.length==0){
        callback(succupload);
      }else{
        upload(null,callback);
      }      
    },
    fail(res){
      errnum++;
      upload()
    }
  })
}
var isfinished=function(){
  if (willupload.length==0){
    return true;
  }else{
    return false;
  }
}
var initupload=function(){
  succupload=[];
  willupload = [];
  inupload = false;
  errnum = 0;
}
module.exports={
  upload: upload,
  isfinished: isfinished,
  initupload: initupload
}
